import java.util.ArrayList;
import java.util.Collections;
import java.util.concurrent.CopyOnWriteArrayList;
import java.util.concurrent.CountDownLatch;

/**
 * Created with IntelliJ IDEA.
 * Description:
 */
public class Demo5 {
    public static void main(String[] args) throws InterruptedException {
        Collections.synchronizedList(new ArrayList<>());
        CopyOnWriteArrayList
        //创建计数器，初始值为10
        CountDownLatch latch = new CountDownLatch(10);
        for (int i = 0; i < 10; i++) {
            Thread t1 = new Thread(() -> {
                try {
                    //模拟执行任务的耗时
                    Thread.sleep(1000);
                    //任务执行完毕，计数器减一
                    latch.countDown();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            });
            t1.start();
        }
        //当计数器不为0时，阻塞当前线程，直到计数器为0，才继续执行后续代码
        latch.await();
        System.out.println("所有任务执行完毕");
    }
}
